package com.example.springboot159.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.springboot159.util.HttpClientHelper;
import org.apache.commons.lang.StringUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class DeleteMediaId {
    public static void main(String[] args) {
        String path = "E:\\mediaId3.txt";

        Map<String, String> map = new HashMap<>();
        BufferedReader in;
        String str;
        try {
            in = new BufferedReader(new FileReader(path));
            while ((str = in.readLine()) != null) {
                if (StringUtils.isNotBlank(str)) {
                    String tmp = str.substring(str.indexOf('"') + 1, str.length() - 1);
                    map.put(tmp, tmp);
                    System.out.println(tmp);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        for (String mediaId : map.values()) {
            String url = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=";
            Map<String, String> params = new HashMap<>();
            params.put("media_id", mediaId);
            System.out.println("参数 " + JSON.toJSONString(params));
            String result = null;
            try {
                result = HttpClientHelper.sendPost(url + getToken(), JSON.toJSONString(params), "UTF-8");
                JSONObject jsonObject = JSON.parseObject(result);
                if (jsonObject.getInteger("errcode").equals(40001)) {
                    result = HttpClientHelper.sendPost(url + getToken(), JSON.toJSONString(params), "UTF-8");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println(result);
        }

    }

    public static String getToken() {
        try {
            String url = "http://wx.10086.cn/operationplatform/wechat/getToken";
            JSONObject resultJson = JSON.parseObject(HttpClientHelper.sendGet(url, null, "UTF-8"));
            return resultJson.getString("bean");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
